<?php

class Config
{
    public $dbconn = null;
    public $ispring_services = null;
    public $ispringcrm_services = null;
    public $statistics = null;
    public $products = null;
    public $actual_products = null;
    public $licenses = null;
    public $isonline_services = null;
    public $server_url = null;
    public $emails = null;
    public $links = null;
    public $site_id = null;
    public $paypal = null;

    const DEFAULT_SUPPORT_EMAIL             = "support@ispringsolutions.com";
    const DEFAULT_PURCHASE_FEEDBACK_EMAIL   = "support@ispringsolutions.com";
    const DEFAULT_SALES_EMAIL               = "sales@ispringsolutions.com";
    const DEFAULT_ADMIN_EMAIL               = "julia.nikolaeva@ispringsolutions.com";

    const DEFAULT_SUPPORT_LINK = "/support/contact.html";
    const DEFAULT_SALES_LINK   = "/company/contact.html";

    public function __construct( $filename, $cache = null )
    {
        $loaded = false;
        if ( !is_null($cache) )
        {
            $loaded = $this->LoadFromCache($cache);
        }

        if ( !$loaded )
        {
            $loaded = $this->LoadFromXMLFile($filename);
            if ( !$loaded )
            {
                throw new Exception("Error while parsing config file '$filename'");
            }

            //$this->links->supportLink = self::DEFAULT_SUPPORT_LINK;
            //$this->links->salesLink = self::DEFAULT_SALES_LINK;

            if ( !is_null($cache) )
            {
                $this->SaveToCache($cache);
            }
        }
    }

    private function LoadFromXMLFile( $filename )
    {
        $xml = @file_get_contents( $filename );
        $doc = Util::CreateXmlDocument($xml, true);
        $xpath = new XPathHelper( $doc );

        $dbconnNode = $xpath->GetNode("dbconn");
        if ( !empty($dbconnNode) )
        {
            $this->dbconn = XMLHelper::CreateObjectFromDOMNode( $xpath, $dbconnNode );
        }

        $servicesNode = $xpath->GetNode("ispring-services");
        $this->ispring_services = XMLHelper::CreateObjectFromDOMNode( $xpath, $servicesNode );

        $ispringCRMServicesNode = $xpath->GetNode("ispringcrm-services");
        $this->ispringcrm_services = XMLHelper::CreateObjectFromDOMNode( $xpath, $ispringCRMServicesNode );

        $statsNode = $xpath->GetNode("statistics");
        $this->statistics = XMLHelper::CreateObjectFromDOMNode( $xpath, $statsNode );

        $this->products = array();
        $this->actual_products = array();
        $nodes = $xpath->GetNodes("products/product[@id]");
        foreach ( $nodes as $node )
        {
            $id = $node->getAttribute("id");
            $actual = $node->getAttribute("actual");
            $this->products[$id] = $xpath->GetText("", $node);
            if ( !empty($actual) )
            {
                $this->actual_products[$id] = $actual;
            }
        }

        $this->licenses = array();
        $nodes = $xpath->GetNodes("licenses/license[@id]");
        foreach ( $nodes as $node )
        {
            $id = $node->getAttribute("id");
            $this->licenses[$id] = $xpath->GetText("", $node);
        }

        $isonlineServicesNode = $xpath->GetNode("ispring-online-services");
        $this->isonline_services = XMLHelper::CreateObjectFromDOMNode( $xpath, $isonlineServicesNode );

        $statsNode = $xpath->GetNode("server-url");
        $this->server_url = XMLHelper::CreateObjectFromDOMNode( $xpath, $statsNode );

        if ( $this->server_url->use_cdn === "true" )
        {
            $this->server_url->content_server_url = $this->server_url->cdn_server_url;
        }

        $this->LoadEmails($xpath);

        $paypalNode = $xpath->GetNode("paypal");
        if ($paypalNode)
        {
            $this->paypal = XMLHelper::CreateObjectFromDOMNode( $xpath, $paypalNode );
        }

        return $this->Validate();
    }

    /**
     * Loads emails from config file.
     *
     * @param XPathHelper $xpath
     */
    private function LoadEmails(XPathHelper $xpath)
    {
        $emailNode = $xpath->GetNode("emails");
        $this->emails = XMLHelper::CreateObjectFromDOMNode( $xpath, $emailNode );

        $this->LoadDefaultEmails();
    }

    /**
     * Loads default value for emails item if it's empty.
     *
     */
    private function LoadDefaultEmails()
    {
        if (empty($this->emails->support_email))
            $this->emails->support_email = self::DEFAULT_SUPPORT_EMAIL;
        if (empty($this->emails->purchase_feedback_email))
            $this->emails->purchase_feedback_email = self::DEFAULT_PURCHASE_FEEDBACK_EMAIL;
        if (empty($this->emails->sales_email))
            $this->emails->sales_email = self::DEFAULT_SALES_EMAIL;
        if (empty($this->emails->admin_email))
            $this->emails->admin_email = self::DEFAULT_ADMIN_EMAIL;
    }

    private function LoadFromCache( ICache $cache )
    {
        $data = $cache->Get("config");
        if ( empty($data) )
        {
            return false;
        }

        foreach ( $data as $name => $value )
        {
            $this->{$name} = $data[$name];
        }

        return $this->Validate();
    }

    private function SaveToCache( ICache $cache )
    {
        $data = array();
        $fields = get_object_vars( $this );
        foreach ( $fields as $name => $value )
        {
            $data[$name] = $value;
        }
        $cache->Set("config", $data);
    }

    private function Validate()
    {
        return !is_null($this->ispring_services)
            && !is_null($this->statistics)
            && !empty($this->products)
            && !is_null($this->actual_products)
            && !empty($this->licenses)
            && !empty($this->server_url);
    }
}

?>
